<template>
  <div class="app-container">
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="再次确认密码">
          <el-input v-model="form.password1" placeholder="请再次输入密码" />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="年龄">
          <el-input-number v-model="form.age" :min="1" :max="100" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form.sex">
            <el-radio label="男" />
            <el-radio label="女" />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="出生日期">
          <el-date-picker v-model="form.birthday" type="date" placeholder="请选择出生日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.note" type="textarea" />
        </el-form-item>
        <el-form-item>
          <el-button v-if="operatingType === 'add'" type="primary" @click="add">Create</el-button>
          <el-button v-else-if="operatingType === 'edit'" type="primary" @click="edit">Create</el-button>
          <el-button @click="onCancel">Cancel</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from '../../utils/request'
export default {
  props: ['operatingType'],
  data() {
    return {
      dialogVisible: false,
      form: {
        username: '',
        password: '',
        password1: '',
        name: '',
        age: 1,
        sex: '男',
        birthday: '',
        note: ''
      }
    }
  },
  methods: {
    openDialog() {
      this.dialogVisible = true
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    onSubmit() {
      var that = this
      console.log(that)
      post('addUser', {
        username: that.form.username,
        password: that.form.password,
        name: that.form.name,
        age: that.form.age,
        sex: that.form.sex,
        birthdayStr: that.form.birthday,
        note: that.form.note
      }, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }})
        .then(response => {
          console.log(response)
          if (response) {
            this.$message.success('添加成功')
            // this.initList()
          } else {
            this.$message.error('添加失败')
          }
        }).catch(function(err) {
          console.log(err)
        })
    },
    onCancel() {
      this.dialogVisible = false
      this.$message({
        message: '取消操作',
        type: 'warning'
      })
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>

